package com.jy.modules.cims.component.jieyue;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.CimsIrtUrlConstant;
import com.jy.modules.cims.common.open.api.enums.EntryResponseEnum;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.component.loan.TBJCLoanImpl;
import com.jy.modules.cims.data.dao.LoanDao;
import com.jy.modules.cims.data.dto.LoanDetailDto;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.response.ResponseLoanResultSearchDTO;

@Service("com.jy.modules.cims.component.jieyue.CSynProcessLoanResultImpl")
public class CSynProcessLoanResultImpl implements ICSynProcess {


	private static final Logger logger = LoggerFactory.getLogger(CSynProcessLoanResultImpl.class);
	
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;

	@Autowired
	private LoanDao loanDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.TBJCLoanImpl")
	private TBJCLoanImpl tbjCLoanImpl;
	
	/**
	 * @Description 根据机构编码，查询系统需要同步更新放款状态的贷款明细。 调用外围系统接口查询放款结果。更新系统贷款明细的贷款状态。
	 * @param cooperorgCode
	 *            机构编码
	 * @throws Exception
	 * @author zhangmi
	 * @date 2016-10-10
	 */
	@Override
	public void doSomething(String cooperorgCode) throws Exception {
		logger.info("查询放款结果开始>>");
		LoanDetailDto searchDto = new LoanDetailDto();
		searchDto.setCooperorgCode(cooperorgCode);//合作机构
		searchDto.setLoanStatus(CimsConstant.LOAN_CONFIRM_WIN);//贷款状态-审批通过
		searchDto.setLoanEachStatus(null);//贷款交互状态-
		List<LoanDetailDto> list = loanDao.searchLoanByCooCodeAndSatus(SearchParamUtil.getSearchParam(searchDto));
		if(CollectionsUtils.isNotEmpty(list)){
			String contractNo = null;
			for (LoanDetailDto loanDetailDto : list) {
				contractNo = loanDetailDto.getContractNo();
				JieYueResponseData responseData = jieYueSend.searchAuditResult(cooperorgCode, contractNo);
				if(responseData!=null){
					if(EntryResponseEnum.SUCESS21001.getCode().equals(responseData.getRetCode())){
						ResponseLoanResultSearchDTO result = (ResponseLoanResultSearchDTO)responseData.getData();
						Date fundDate = result.getEndTime();//放款时间
						if(CimsConstant.JIEYUE_RESULT_API_LOAN_SUCCESS.equals(result.getResult())){
							tbjCLoanImpl.loanFundOut(contractNo, CimsIrtUrlConstant.IRT_LOAN_FUNDOUT_SUCCESS,fundDate);
							tbjCLoanImpl.auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_CORE, CimsConstant.LOAN_FUNDOUT_WIN,fundDate);
							tbjCLoanImpl.insertCreditByContractNo(contractNo, fundDate);
						}else if(CimsConstant.JIEYUE_RESULT_API_LOAN_REFUSE.equals(result.getResult())){
							tbjCLoanImpl.loanFundOut(contractNo, CimsIrtUrlConstant.IRT_LOAN_APPROVE_FAIL,fundDate);
							tbjCLoanImpl.auditLoan(contractNo, CimsConstant.LOAN_EACH_STATUS_CORE, CimsConstant.LOAN_FUNDOUT_LOSE,null);
						}else{
							logger.info("合作机构【{}】，查询放款结果异常编码【{}】，异常描述【{}】，合同号【{}】！",cooperorgCode,result.getResult(),result.getMessage(),contractNo);
						}
					}else{
						logger.info("合作机构【{}】，查询放款结果接口异常编码【{}】，异常描述【{}】，合同号【{}】！",cooperorgCode,responseData.getRetCode(),responseData.getMessage(),contractNo);
					}
				}else{
					logger.info("合作机构【{}】，查询审批结果接口，返回数据为空！",cooperorgCode);
				}
			}
		}else{
			logger.info("合作机构【{1}】，没有需要查询放款结果的数据！",cooperorgCode);
		}
		logger.info("查询放款结果结束>>");
	}

}
